/*
 * @Author: your name
 * @Date: 2020-03-30 17:19:16
 * @LastEditTime: 2020-04-02 22:08:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-imooc-ebook\src\utils\localStorage.js
 */
// 这个库的好处就是，可以将我们传入的对象变为JSON经行存储，当我们读取时，又可以将JSON字符串在转化为对象
import Storage from 'web-storage-cache'

const localStorage = new Storage()
// 写入数据
export function setLocalStorage(key, value) {
  return localStorage.set(key, value)
}
// 获取数据
export function getLocalStorage(key) {
  return localStorage.get(key)
}
// 删除一行数据
export function removeLocalStorage(key) {
  return localStorage.delete(key)
}
// 删除所有数据
export function clearLocalStorage() {
  return localStorage.clear()
}

export function saveBookShelf(shelf) {
  return setLocalStorage('shelf', shelf)
}

export function getBookShelf() {
  return getLocalStorage('shelf')
}
// 每一本电子书存储的内容或则是设置都是不一样的
// 为每一本电子书，都开一个localstorage空间
export function setBookObject(fileName, key, value) {
  // info表示一些设置信息
  let book = getLocalStorage(`${fileName}-info`)
  if (!book) {
    book = {}
  }
  book[key] = value
  // console.log('setBookObject', book)
  // book存储时候会是一个JSON，获取时候转化为一个对象
  setLocalStorage(`${fileName}-info`, book)
}

export function getBookObject(fileName, key) {
  const book = getLocalStorage(`${fileName}-info`)
  if (book) {
    return book[key]
  } else {
    return null
  }
}
// 存储字体
export function getFontFamily(fileName) {
  return getBookObject(fileName, 'fontFamily')
}
// 设置字体
export function saveFontFamily(fileName, font) {
  return setBookObject(fileName, 'fontFamily', font)
}

export function getLocale() {
  return getLocalStorage('locale')
}
// startCfi = currentLocation.start.cfi当前页的第一个字符位置
export function saveLocale(locale) {
  return setLocalStorage('locale', locale)
}

export function getLocation(fileName) {
  return getBookObject(fileName, 'location')
}

export function saveLocation(fileName, location) {
  setBookObject(fileName, 'location', location)
}

export function getBookmark(fileName) {
  return getBookObject(fileName, 'bookmark')
}

export function saveBookmark(fileName, bookmark, id) {
  setBookObject(fileName, 'bookmark', bookmark, id)
}

export function getReadTime(fileName) {
  return getBookObject(fileName, 'time')
}
// 秒计数,半分钟存到lc中一次
export function saveReadTime(fileName, readTime) {
  // console.log('saveReadTime', readTime)
  setBookObject(fileName, 'time', readTime)
}

export function getProgress(fileName) {
  return getBookObject(fileName, 'progress')
}

export function saveProgress(fileName, progress) {
  setBookObject(fileName, 'progress', progress)
}

export function getNavigation(fileName) {
  return getBookObject(fileName, 'navigation')
}

export function saveNavigation(fileName, navigation) {
  setBookObject(fileName, 'navigation', navigation)
}

export function getMetadata(fileName) {
  return getBookObject(fileName, 'metadata')
}

export function saveMetadata(fileName, metadata) {
  setBookObject(fileName, 'metadata', metadata)
}

export function getCover(fileName) {
  return getBookObject(fileName, 'cover')
}

export function saveCover(fileName, cover) {
  setBookObject(fileName, 'cover', cover)
}

export function getTheme(fileName) {
  return getBookObject(fileName, 'theme')
}

export function saveTheme(fileName, theme) {
  setBookObject(fileName, 'theme', theme)
}

export function getFontSize(fileName) {
  return getBookObject(fileName, 'fontSize')
}

export function saveFontSize(fileName, fontSize) {
  setBookObject(fileName, 'fontSize', fontSize)
}
